<?php

namespace app\admin\model;

//加载配置类
use think\Config;
//导入Model
use think\Model;
//导入Db
use think\Db;
//引入Session类
use think\Session;

class AdminUserModel extends Model
{
	/**
	*	获取后台管理员列表
	*/
	public function getAdminuser($data)
	{
		$where = [];

		/* 用户名 */
		$username = empty($data['username']) ? '' : $data['username'];
		$username = trim($username);
		if(!empty($username)){
			$where['username'] = $username;
		}

		/* 状态 */
		$status = empty($data['status']) ? '' : $data['status'];
        if(!empty($status)){
        	$where['status'] = $status;
        }

		$res = Db::table('sz_adminuser')->where($where)->paginate(10,false,['query' => request()->param()]);

		return $res;
	}

	/**
	*	添加管理员提交
	*/
	public function addAdminUser($data)
	{
		$data['password'] = md5($data['password']);
		if(!empty($data)){
			$result = Db::table('sz_adminuser')->where('username',$data['username'])->find();
			if(empty($result)){
				$res = Db::table('sz_adminuser')->insert($data);
				return $res;
			}else{
				return 2;
			}
			
		}
	}

	/**
	*	编辑管理员信息
	*/
	public function getOne($id)
	{
		if(!empty($id)){
			$res = Db::table('sz_adminuser')->where('id',$id)->find();
			return $res;
		}
	}

	/**
	*	编辑管理员信息提交
	*/
	public function editAdminUser($id,$data)
	{
		if(!empty($data)){
			$res = Db::table('sz_adminuser')->where('id',$id)->update($data);
			return $res;
		}
	}

	/**
	*	删除管理员
	*/
	public function deleteAdminUser($id)
	{
		if(!empty($id)){
			$res = Db::table('sz_adminuser')->where('id',$id)->delete();
			return $res;
		}
	}

	/**
	*	禁用管理员
	*/
	public function banAdminUser($id)
	{
		if(!empty($id)){
			$res = Db::table('sz_adminuser')->where('id',$id)->find();
			if($res['status'] == 1){
				$res = Db::table('sz_adminuser')->where('id',$id)->update(['status'=>2]);
				return $res;
			}
		}
	}

	/**
	*	启用管理员
	*/
	public function cancelBanAdminUser($id)
	{
		if(!empty($id)){
			$res = Db::table('sz_adminuser')->where('id',$id)->find();
			if($res['status'] == 2){
				$res = Db::table('sz_adminuser')->where('id',$id)->update(['status'=>1]);
				return $res;
			}
		}
	}
}